package com.mashibing.juc.c_020_Interview;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * 使用Semaphore方式处理两个线程工作问题（一个线程添加对象，另外一个线程实现监控）
 * t1中控制t2
 * @author: Yuanzt
 * @Date: 2021/3/30
 */
public class T08_Semaphore {

    // 添加volatile，使t2能够得到通知
    volatile List lists = new ArrayList();

    static Thread t1 = null ,t2 = null;

    public void add(Object o) {
        lists.add(o);
    }

    public int size() {
        return lists.size();
    }

    public static void main(String[] args) {

        T08_Semaphore c = new T08_Semaphore();
        Semaphore s = new Semaphore(1);
        t1 = new Thread(()->{
            try {
                s.acquire();
                for (int i = 0; i < 5; i++) {
                    c.add(new Object());
                    System.out.println("add " + i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                s.release();
            }

            try {
                t2.start();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                s.acquire();
                for (int i = 5; i < 10; i++) {
                    c.add(new Object());
                    System.out.println("add " + i);
                }
                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1");

        t2 = new Thread(()->{
            try {
                s.acquire();
                System.out.println("t2 结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                s.release();
            }

        });

        t1.start();

    }

}
